# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pdal
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.5.6
pkgrel=1
pkgdesc="A C++ library for translating and manipulating point cloud data (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.pdal.io"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gdal")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-hdf5")
            #"${MINGW_PACKAGE_PREFIX}-tiledb")
optdepends=("${MINGW_PACKAGE_PREFIX}-hdf5: HDF plugin, read data in the HDF format"
            "${MINGW_PACKAGE_PREFIX}-tiledb: TileDB plugin, read/write data from TileDB")
source=("https://github.com/PDAL/PDAL/releases/download/${pkgver}/PDAL-${pkgver}-src.tar.gz"
        0001-cmake-disable-backtrace-unwind.patch)
sha256sums=('c4f51e7bb9843ecc5d51c47aad87d8ddbd919111fb49222294c5835be7c1fa85'
            '94638e8db55b3815c976643b73be114e71018fd8edb699b1488e4ddcd1583f75')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd PDAL-${pkgver}-src

  apply_patch_with_msg \
    0001-cmake-disable-backtrace-unwind.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -Wno-dev \
    -GNinja \
    ${extra_config[@]} \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DBUILD_SHARED_LIBS=ON \
    -DWITH_TESTS=OFF \
    -DBUILD_PLUGIN_HDF=ON \
    -DBUILD_PLUGIN_TILEDB=OFF \
    ../PDAL-${pkgver}-src

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/PDAL-${pkgver}-src/LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
